/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import model.TaiKhoan;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class TaiKhoanDao {
    private static Session session;
    
    public TaiKhoanDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertTaiKhoan function
     */
    public boolean insertTaiKhoan(String tenNguoiDung, Boolean isAdmin, String tenDangNhap, String matKhau){
        try {
            session.beginTransaction();
            
            TaiKhoan taiKhoan = new TaiKhoan(tenNguoiDung, isAdmin, tenDangNhap, matKhau);
            
            session.save(taiKhoan);
            session.flush();
            session.getTransaction().commit();
            
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
    
    /*
     * changePassword function
     */
    public boolean changePassword(int id, String matKhau, Boolean isAdmin){
        try {
            session.beginTransaction();
            
            TaiKhoan taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, id);
            taiKhoan.setMatKhau(matKhau);
            taiKhoan.setIsAdmin(isAdmin);
            
            session.update(taiKhoan);
            session.flush();
            session.getTransaction().commit();
            
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
    
    /*
     * deleteTaiKhoan function
     */
    public boolean deleteTaiKhoan(int id){
        try {
            session.beginTransaction();
            
            TaiKhoan taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, id);
            
            session.delete(taiKhoan);
            session.flush();
            session.getTransaction().commit();
            
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
    
    /*
     * searchByNguoiDung function
     */
    public List<TaiKhoan> searchByNguoiDung(String nguoiDung){
        try {
            session.beginTransaction();
            
            String sql = "FROM TaiKhoan WHERE tenNguoiDung like?";
            Query query = session.createQuery(sql);
            query.setString(0, "%" + nguoiDung + "%");
            List<TaiKhoan> list = query.list();
            
            session.getTransaction().commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    } 
    
    public List<TaiKhoan> searchByAdmin(Boolean isAdmin){
        try {
            session.beginTransaction();
            
            String sql = "FROM TaiKhoan WHERE tenNguoiDung like?";
            Query query = session.createQuery(sql);
            query.setString(0, "%" + isAdmin + "%");
            List<TaiKhoan> list = query.list();
            
            session.getTransaction().commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /*
     * getAllData function
     */
    public List<TaiKhoan> getAllData(){
        try {
            session.beginTransaction();
            
            String sql = "from TaiKhoan";
            Query query = session.createQuery(sql);
            List<TaiKhoan> list = query.list();
            
            session.getTransaction().commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
